/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.mq.main;

import cn.ac.iie.di.wx.mq.common.RuntimeEnv;
import static cn.ac.iie.di.wx.mq.common.RuntimeEnv.mqredisVos;
import cn.ac.iie.di.wx.mq.httpservice.HttpSender;
import static cn.ac.iie.di.wx.mq.main.QmCplMain.init;
import cn.ac.iie.di.wx.mq.redis.JedisThread;
import cn.ac.iie.di.wx.mq.vo.RedisVo;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.log4j.Logger;

/**
 * 群聊多媒体消息拉取队列的补全程序 入redis的只能是9月4号以后的数据
 *
 * @author Administrator
 */
public class MqCplMain {

    static Logger logger = Logger.getLogger(MqCplMain.class.getName());

    public static void main(String[] args) throws FileNotFoundException, IOException {
        try {
            //读取日志文件，筛选出
            //获取根路径
            init();
            JedisThread jedisThread = new JedisThread(RuntimeEnv.mqredisVos);
            Thread thread = new Thread(jedisThread);
            thread.start();

            logger.info("start redis ,host 10.144.32.21:26379;10.144.32.22:26379,master :mymaster, dbname:0");

            String root = System.getProperty("user.dir");
            String absolutePath = root + "/" + (String) (RuntimeEnv.getParam(RuntimeEnv.cplLogFileName));
            logger.info("从" + absolutePath + "恢复开始");
            FileReader reader = new FileReader(absolutePath);
            BufferedReader br = new BufferedReader(reader);

            String str = "";
            while ((str = br.readLine()) != null) {
                Thread.sleep(1000);//一秒钟发一个。。。
                String request = str.split(",send to")[0].split("request is : ")[1];
                //不入redis，因为之前已经入过一次，且设置了key的过过期时间
//                String response = str.split(",response is ")[1];
                JSONObject requestJson = JSONObject.parseObject(request);
                String opid = requestJson.getString("opid");//存redis里，将关键字存value里面
                String g_id_time = str.split(",g_id_time is ")[1];
                logger.info("请求为:" + request);
                logger.info("g_id_time :" + g_id_time);
                boolean flag = mqredisVos.offer(new RedisVo(opid, g_id_time));
                logger.info("放入redis队列" + "opid=" + opid + ",g_id_time=" + g_id_time + ",是否成功：" + flag);
//                logger.info("原响应为：" + response);
                String sec_response = HttpSender.sendStringByUrl(request, (String) RuntimeEnv.getParam(RuntimeEnv.groupmediaURL));
                logger.info("重新发送，响应为：" + sec_response);

                //转换成json对象
                try {
                    JSONObject jSONObject = JSONObject.parseObject(sec_response);
                    //已经正常下发，不需要重新发送
                    if (!jSONObject.getString("status").equals("200")) {
                        logger.info("返回值不是200，重新发送" + request);
                        logger.info("因返回值不为200重新进行发送，响应为：" + HttpSender.sendStringByUrl(request, "http://15.202.1.4:8090/lokiRest/service/wxservice/querygroupfile.json"));
                    }
                } catch (Exception e) {
                    logger.error("this response is not a json object" + e.getMessage(), e);
                    //c重新发送
//                    if (response.equals("")) {
//                        //重新发送
//                        logger.info("返回值为空，重新发送" + request);
//                        logger.info("因返回值为空(烽火超时)重新进行发送，响应为：" + HttpSender.sendStringByUrl(request, "http://15.202.1.4:8090/lokiRest/service/wxservice/querygroupfile.json"));
//
//                    }
                }
            }
            br.close();
            reader.close();
            //截取关键字段，判断是否是异常返回，如果是，则调用补全接口重新发送一次。
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
}
